home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / SmallEiffel 0.3.3 / SmallEiffel PPC / lib_std / std_file_write.e < prev    next >
Encoding:
Text File  |  1996-06-13  |  3.8 KB  |  197 lines  |  [TEXT/EDIT]

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. class STD_FILE_WRITE
  5. --
  6. -- Basic output facilities to write a named file on the disk.
  7. --
  8. -- Note : most features are common with STD_OUTPUT so you can 
  9. --        test your program first on the screen and then, changing 
  10. --        of instance (STD_OUTPUT/STD_FILE_WRITE), doing the same
  11. --        on a file.
  12. --
  13.    
  14. inherit STD_FILE
  15.    
  16. creation connect_to
  17.    
  18. feature 
  19.  
  20.    connect_to(new_path: STRING) is
  21.       do
  22.      mode := "w";
  23.      output_stream := fopen(new_path,mode);
  24.      if output_stream /= Void then
  25.         path := new_path;
  26.      end;
  27.       end;
  28.    
  29.    disconnect is
  30.       local
  31.      err: INTEGER;
  32.       do
  33.      err := fclose(output_stream); 
  34.      path := Void;
  35.       end;
  36.    
  37. feature    
  38.    
  39.    put_character(c: CHARACTER) is
  40.       local
  41.      err: CHARACTER;
  42.       do
  43.      err := fputc(c,output_stream);
  44.      if err /= c then
  45.         std_error.put_string("Error while writing character."); 
  46.         crash;
  47.      end;
  48.       end;
  49.  
  50.    put_string(s: STRING) is
  51.      -- Output `s' to current output device.
  52.       require
  53.      s /= Void;
  54.       local
  55.      i: INTEGER;
  56.       do
  57.      from  
  58.         i := 1;
  59.      until
  60.         i > s.count
  61.      loop
  62.         put_character(s.item(i));
  63.         i := i + 1;
  64.      end;
  65.       end;
  66.    
  67.    put_integer (i: INTEGER) is
  68.      -- Output `i' to current output device.
  69.       do
  70.      tmp_string.clear;
  71.      i.append_in(tmp_string);
  72.      put_string(tmp_string);
  73.       end;
  74.    
  75.    put_integer_format(i, s: INTEGER) is
  76.      -- Output `i' to current output device using at most
  77.      -- `s' character.
  78.       do
  79.      tmp_string.clear;
  80.      i.append_in_format(tmp_string,s);
  81.      put_string(tmp_string);
  82.       end;
  83.    
  84.    put_real(r: REAL) is
  85.      -- Output `r' to current output device.
  86.       do
  87.      tmp_string.clear;
  88.      r.append_in(tmp_string);
  89.      put_string(tmp_string);
  90.       end;
  91.    
  92.    put_real_format(r: REAL; f: INTEGER) is
  93.      -- Output `r' with only `f' digit for the fractionnal part.
  94.      -- Examples: 
  95.      --    put_real(3.519,2) print "3.51". 
  96.       require
  97.      f >= 0;
  98.       do
  99.      tmp_string.clear;
  100.      r.append_in_format(tmp_string,f);
  101.      put_string(tmp_string);
  102.       end;
  103.    
  104.    put_double(d: DOUBLE) is
  105.      -- Output `d' to current output device.
  106.       do
  107.      tmp_string.clear;
  108.      d.append_in(tmp_string);
  109.      put_string(tmp_string);
  110.       end;
  111.    
  112.    put_double_format(d: DOUBLE; f: INTEGER) is
  113.      -- Output `d' with only `f' digit for the fractionnal part.
  114.      -- Examples: 
  115.      --    put_double(3.519,2) print "3.51". 
  116.       require
  117.      f >= 0;
  118.       do
  119.      tmp_string.clear;
  120.      r.append_in_format(tmp_string,f);
  121.      put_string(tmp_string);
  122.       end;
  123.    
  124.    put_boolean(b: BOOLEAN) is
  125.      -- Output `b' to current output device according
  126.      -- to the Eiffel format.
  127.       do
  128.      if b then
  129.         put_string("true");
  130.      else
  131.         put_string("false");
  132.      end;
  133.       end;
  134.    
  135.    put_new_line is
  136.      -- Output a newline character.
  137.       do
  138.      put_character('%N');
  139.       end;
  140.    
  141.    put_spaces(nb: INTEGER) is
  142.       -- Output `nb' spaces character.
  143.       require
  144.      nb >= 0;
  145.       local
  146.      count : INTEGER;
  147.       do
  148.      from  
  149.      until
  150.         count >= nb
  151.      loop
  152.         put_character(' ');
  153.         count := count + 1;
  154.      end;
  155.       end; 
  156.    
  157.    append_file(file_name: STRING) is
  158.       require
  159.      file_exists(file_name);
  160.       local
  161.      c: CHARACTER;
  162.       do
  163.      tmp_file_read.connect_to(file_name);
  164.      from  
  165.         tmp_file_read.read_character;
  166.      until
  167.         tmp_file_read.end_of_input
  168.      loop
  169.         c := tmp_file_read.last_character;
  170.         put_character(c);
  171.         tmp_file_read.read_character;
  172.      end;
  173.      tmp_file_read.disconnect;
  174.       end;
  175.    
  176. feature {NONE}
  177.    
  178.    tmp_file_read: STD_FILE_READ is
  179.       once
  180.      !!Result.make;
  181.       end;
  182.    
  183. feature {NONE}
  184.    --
  185.    -- NOTE: use only a few basic ANSI C functions.
  186.    -- Try to use as few external C calls as possible.
  187.    --
  188.    
  189.    output_stream: POINTER;
  190.    
  191.    tmp_string: STRING is
  192.       once
  193.      !!Result.make(512);
  194.       end;
  195.    
  196. end -- STD_FILE_WRITE
  197.